package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcYingShiConfigMapper;
import com.csun.cmny.provider.device.yingshi.YingShiUtils;
import com.csun.cmny.provider.device.yingshi.entity.YingShiAccessTokenVo;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcYingShiConfig;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcYingShiConfigService;
import com.csun.cmny.util.PublicUtil;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021/8/27 16:49
 * @Description: 萤石摄像头应用信息接口实现
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcYingShiConfigServiceImpl extends
        BaseService<DmcYingShiConfig> implements DmcYingShiConfigService {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcYingShiConfigMapper dmcYingShiConfigMapper;
    @Resource
    private YingShiUtils yingShiUtils;

    /**
     * 保存萤石应用信息
     *
     * @param dmcYingShiConfig  the dmc ying shi config
     * @param loginAuthDto      the login auth dto
     *
     * @return  the int.
     */
    @Override
    public Integer save(DmcYingShiConfig dmcYingShiConfig, LoginAuthDto loginAuthDto) {

        if (PublicUtil.isEmpty(dmcYingShiConfig.getGroupId())) {
            dmcYingShiConfig.setGroupId(loginAuthDto.getGroupId());
        }

        DmcYingShiConfig record = selectByKey(dmcYingShiConfig.getGroupId());
        if (PublicUtil.isNotEmpty(record)) {
            if (PublicUtil.isNotEmpty(dmcYingShiConfig.getAccount())) {
                record.setAccount(dmcYingShiConfig.getAccount());
            }
            if (PublicUtil.isNotEmpty(dmcYingShiConfig.getPassword())) {
                record.setPassword(dmcYingShiConfig.getPassword());
            }
            if (PublicUtil.isNotEmpty(dmcYingShiConfig.getAppKey())) {
                record.setAppKey(dmcYingShiConfig.getAppKey());
            }
            if (PublicUtil.isNotEmpty(dmcYingShiConfig.getSecret())) {
                record.setSecret(dmcYingShiConfig.getSecret());
            }

            YingShiAccessTokenVo yingShiAccessTokenVo = yingShiUtils
                    .getAccessToken(record.getAppKey(), record.getSecret());

            if (PublicUtil.isEmpty(yingShiAccessTokenVo)) {
                throw new DmcBizException(ErrorCodeEnum.DMC100622003);
            }

            record.setAccessToken(yingShiAccessTokenVo.getAccessToken());
            record.setExpireTime(new Date(yingShiAccessTokenVo.getExpireTime()));

            record.setLastOperator(loginAuthDto.getUserName());
            record.setLastOperatorId(loginAuthDto.getUserId());
            record.setUpdateTime(new Date());

            return dmcYingShiConfigMapper.updateByPrimaryKeySelective(record);
        } else {
            // 校验萤石账号不能为空
            Preconditions.checkArgument(PublicUtil.isNotEmpty(dmcYingShiConfig.getAccount()),
                    cmnyMessageSource.getMessage("ying.shi.account.null", null));
            // 校验萤石密码不能为空
            Preconditions.checkArgument(PublicUtil.isNotEmpty(dmcYingShiConfig.getPassword()),
                    cmnyMessageSource.getMessage("ying.shi.password.null", null));
            // 校验萤石应用key不能为空
            Preconditions.checkArgument(PublicUtil.isNotEmpty(dmcYingShiConfig.getAppKey()),
                    cmnyMessageSource.getMessage("ying.shi.app.key.null", null));
            // 校验萤石应用密码不能为空
            Preconditions.checkArgument(PublicUtil.isNotEmpty(dmcYingShiConfig.getSecret()),
                    cmnyMessageSource.getMessage("ying.shi.secret.null", null));

            YingShiAccessTokenVo yingShiAccessTokenVo = yingShiUtils
                    .getAccessToken(dmcYingShiConfig.getAppKey(), dmcYingShiConfig.getSecret());

            if (PublicUtil.isEmpty(yingShiAccessTokenVo)) {
                throw new DmcBizException(ErrorCodeEnum.DMC100622003);
            }

            dmcYingShiConfig.setAccessToken(yingShiAccessTokenVo.getAccessToken());
            dmcYingShiConfig.setExpireTime(new Date(yingShiAccessTokenVo.getExpireTime()));

            dmcYingShiConfig.setCreator(loginAuthDto.getUserName());
            dmcYingShiConfig.setCreatorId(loginAuthDto.getUserId());
            dmcYingShiConfig.setLastOperator(loginAuthDto.getUserName());
            dmcYingShiConfig.setLastOperatorId(loginAuthDto.getUserId());

            return dmcYingShiConfigMapper.insertSelective(dmcYingShiConfig);
        }
    }

    /**
     * 更新access token
     *
     * @param dmcYingShiConfig    the ying shi config
     *
     * @return  the int.
     */
    @Override
    public Integer updateAccessToken(DmcYingShiConfig dmcYingShiConfig) {

        return dmcYingShiConfigMapper.updateAccessToken(dmcYingShiConfig);
    }
}
